package com.ruoyi.web.controller.topic;


import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.ruoyi.common.config.lphCosConfig;
import com.ruoyi.system.domain.Myuser;
import com.ruoyi.system.domain.Talk;
import com.ruoyi.system.domain.User;
import com.ruoyi.system.domain.dto.TalkBackDTO;
import com.ruoyi.system.domain.dto.TalkDTO;
import com.ruoyi.system.domain.vo.Result;
import com.ruoyi.system.domain.vo.Results;
import com.ruoyi.system.service.TalkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.crypto.Data;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Api(tags = "说说模块")
@RequestMapping("/api")
@RestController
public class TalkController extends BaseController {

    @Autowired
    private TalkService talkService;

    @ApiOperation("查看首页说说")
    @GetMapping("/index/talks/{userid}")
    public Result<List<TalkDTO>> getAllTalks(@PathVariable("userid") Long userid){
        return Result.success(talkService.getAllTalks(userid));
    }

    @ApiOperation("根据id查看说说")
    @ApiImplicitParam(name = "talkId",value = "说说id",required = true,dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/talks/{talkId}/{userid}")
    public Result<TalkDTO> getTalkById(@PathVariable("userid")Long userid,@PathVariable("talkId") Long talkId){
        return Result.success(talkService.getTalkById(userid,talkId));
    }

    @ApiOperation(value = "查看后台说说")
    @GetMapping("/admin/talks")
    public Result<List<TalkBackDTO>> listBackTalks() {
        return Result.success(talkService.listBackTalks());
    }

    @ApiOperation(value = "根据id查看后台说说")
    @ApiImplicitParam(name = "talkId", value = "说说id", required = true, dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/admin/talks/{talkId}/{userid}")
    public Result<TalkBackDTO> getBackTalkById(@PathVariable("userid")Long userid,@PathVariable("talkId") Long talkId) {
        return Result.success(talkService.getBackTalkById(userid,talkId));
    }

    @ApiOperation(value = "点赞说说")
    @ApiImplicitParam(name = "talkId", value = "说说id", required = true, dataType = "Long",dataTypeClass = Long.class)
    @PostMapping("/talks/{talkId}/like/{userid}")
    public Result<?> saveTalkLike(@PathVariable("userid")Long userid,@PathVariable("talkId") Long talkId) {
        int i = talkService.saveTalkLike(userid,talkId);
        return Result.success(i);
    }

    @ApiOperation("查询总计访问量")
    @GetMapping("/footer/talkMessage")
    public Result<Integer> articlesMessage(){
        return Result.success(talkService.getViewTotal());
    }

    @ApiOperation("添加说说")
    @PostMapping("/admin/talks")
    public Result<?> saveTalk(@RequestParam("userid") Integer userid,@RequestParam("content") String content,@RequestParam("status")Integer status,@RequestParam("file") MultipartFile image){
        String originalFilename = image.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        suffix = suffix.toLowerCase();
        if( suffix.equals(".jpg")|| suffix.equals(".png") ||suffix.equals(".jpeg")){
            COSClient cosClient = new lphCosConfig().cosClient();
            String url=null;
            String newfilename= UUID.randomUUID().toString()+suffix;
            ObjectMetadata objectMetadata = new ObjectMetadata();
            try {
                InputStream inputStream = image.getInputStream();
                objectMetadata.setContentLength(inputStream.available());
                PutObjectRequest putObjectRequest1 = new PutObjectRequest(new lphCosConfig().getbucket(),newfilename, inputStream, objectMetadata);
                cosClient.putObject(putObjectRequest1);
                url = "https://" + new lphCosConfig().getbucket() + ".cos.ap-guangzhou.myqcloud.com" + "/" +newfilename;
                Talk talk = new Talk();
                talk.setContent(content);
                talk.setStatus(status);
                talk.setUserId(userid.longValue());
                talk.setImages(url);
                return Result.success(talkService.saveTalk(talk));
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                cosClient.shutdown();
            }
        }
        return Result.fail("添加失败");
    }

    @ApiOperation("修改说说")
    @PostMapping("/admin/talks/{id}")
    public Result<?> updateTalk(@PathVariable("id") Long id,Talk talk){
        return Result.success(talkService.updateTalk(talk));
    }

    @ApiOperation("删除说说")
    @PostMapping("/admin/talk/{userid}/{talkId}/delete")
    public Result<?> deleteTalk(@PathVariable("userId") Long userId,@PathVariable("talkId") Long id){
        TalkDTO talkById = talkService.getTalkById(userId, id);
        COSClient cosClient = new lphCosConfig().cosClient();
        String key  = talkById.getImages().substring(talkById.getImages().lastIndexOf("/"));
        cosClient.deleteObject("teach-1-1314343159",key);
        talkService.deleteTalk(id);

        return Result.success();
    }
    @ApiOperation("根据userId查看说说")
    @ApiImplicitParam(name = "userId",value = "userid",required = true,dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/talks/userid/{userId}")
    public Result<List<TalkDTO>> getTalkByUserId(@PathVariable("userId") Long userId){
        return Result.success(talkService.getTalkByUserId(userId));
    }


}
